﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Mvc;


namespace SampleAPIService.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class HttpTestController : ControllerBase
    {
        private static List<HttpTest> _lst = new List<HttpTest>()
        {
            new HttpTest{ID =1, Name="name1", CreateTime = DateTime.Now},
            new HttpTest{ID =2, Name="name2", CreateTime = DateTime.Now.AddDays(1)},
            new HttpTest{ID =3, Name="name3", CreateTime = DateTime.Now.AddDays(2)},
        };

        [HttpGet]
        public IEnumerable<HttpTest> Get()
        {
            return _lst;
        }

        [HttpGet("{id}")]
        public HttpTest Get(int id)
        {
            return _lst.Where(q => q.ID == id).FirstOrDefault();
        }

        [HttpPost]
        public void Post([FromBody] HttpTest value)
        {
            _lst.Add(value);
        }

        [HttpPut("{id}")]
        public void Put(int id, [FromBody] HttpTest httpTest)
        {
            var httpTestOld = _lst.Where(q => q.ID == id).FirstOrDefault();
            httpTestOld.Name = httpTest.Name;
            httpTestOld.CreateTime = httpTestOld.CreateTime;
        }

        [HttpDelete("{id}")]
        public void Delete(int id)
        {
            var httpTest = _lst.Where(q => q.ID == id).FirstOrDefault();
            _lst.Remove(httpTest);
        }
    }
}
